<?php
date_default_timezone_set("PRC");

require './vendor/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Processing\PersistentFop;

session_start();

if(isset($_POST["username"]) && isset($_POST["password"])):
    if($_POST["username"] == "hills" && sha1($_POST["password"]."vedioGift") == "42a70973d3ae4faa7ecd846e73c722548193d7d6"):
        $_SESSION["NAME"] = $_POST["username"];
    endif;
    
endif;

if(isset($_GET["logout"])):
    unset($_SESSION["NAME"]);
    session_destroy();  
endif;

if(isset($_SESSION["NAME"])):


    $accessKey = 'naQm4T_P4GQeSpOcOvRCT3Ob73Gf2GNhmIEJeo3x';
    $secretKey = 'N0iwl_8zMj_xedBi1KV1f3kADNn9eby1WQFIQu0e';
    $auth = new Auth($accessKey, $secretKey);
    $bucket = 'hills';
    $prefix = '';

    $bucketMgr = new BucketManager($auth);
    //删除
    if(isset($_GET["delete_file"])){
        $err = $bucketMgr->delete($bucket, $_GET["delete_file"]);
        if ($err !== null) {
            var_dump($err);exit;
        }
        header("Location: admin.php");
    }

    //列表
    list($items, $marker, $err) = $bucketMgr->listFiles($bucket, $prefix);

    if ($err !== null) {
        var_dump($err);exit;
    }

    //上传
    $token = $auth->uploadToken($bucket);
    $domain  = 'http://7xkfmq.com1.z0.glb.clouddn.com/';

    //切片
    // $key = 'meipai_20140723220803.mp4';
    // $pfop = new PersistentFop($auth, $bucket);

    // $fops='avthumb/m3u8/segtime/40/vcodec/libx264/s/320x240';
    // list($id, $err) = $pfop->execute($key, $fops);

    // echo "\n====> pfop avthumb result: \n";
    // if ($err != null) {
    //     var_dump($err);
    // } else {
    //     echo "PersistentFop Id: $id";
    // }
 endif
?>

<!doctype html>
<html lang="en">
<head>
    <title>vGift</title>
	<meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
	
	<link href="favicon.ico" rel="shortcut icon">
	<link rel="stylesheet" href="bootstrap/css/bootstrap.css">
	<link rel="stylesheet" href="main.css">
	<link rel="stylesheet" href="js/highlight/highlight.css">

	<!--[if lt IE 9]>
      <script src="js/Respond-1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="container">
<?php if(!isset($_SESSION["NAME"])):?>
    <div class="modal show">
      <div class="modal-dialog">
        <div class="modal-content">
          <form action="admin.php" method="POST">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">用户登陆</h4>
          </div>
          <div class="modal-body">
        
            <div class="form-group">
                <label for="exampleInputEmail1">用户名</label>
                <input class="form-control" type="text"     placeholder="用户名" name="username" />
            </div>
            <div class="form-group">
                <label for="exampleInputEmail1">密码</label>
                <input class="form-control" type="password" placeholder="密码"   name="password" />
            </div>
           
          </div>
          <div class="modal-footer">
            <button type="submit" class="btn btn-primary">登陆</button>
          </div>
          </form>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->


<?php else:?>    

	<div class="text-left col-md-12 wrapper">
		<h1 class="text-left col-md-12" style="border-bottom:none;">
			<img src="logo.png" style="width:40px;">
            vGift
            <span style="font-size:12px;vertical-align:top;" class="label label-default">BETA</span> 
            <a style="font-size:14px;" class="btn btn-default pull-right" href="admin.php?logout=1">登出</a>
		</h1>
        <div class="body">
            <input type="hidden" id="uptoken" value="<?=$token?>">
            <input type="hidden" id="domain" value="<?=$domain?>">
            <div class="col-md-12">
                <div id="container">
                    <a class="btn btn-default btn-lg " id="pickfiles" href="#" >
                        <i class="glyphicon glyphicon-plus"></i>
                        <span>上传新视频</span>
                    </a>
                    
                    <form action="qrcode.php" method="GET" target="_blank" class="form-inline pull-right">
                        <input type="number" class="form-control input-sm" style="width:100px;" placeholder="开始" name="s" />-
                        <input type="number" class="form-control input-sm" style="width:100px;" placeholder="结束" name="e" />
                        <button class="btn btn-default btn-sm" type="submit">生成二维码</button>
                    </form>
                    
                </div>
                <div style="margin:-10px 0 20px;">
                    <p class="label label-default">上传前请先扫描二维码，并将视频文件名称改为二维码中的文件名称。</p>
                </div>
            </div>

            <div style="display:none" id="success" class="col-md-12" >
                <div class="alert-success">
                    队列全部文件处理完毕
                </div>
            </div>
            <div class="col-md-12 ">
                <table class="table table-striped table-hover text-left"   style="margin-top:40px;display:none">
                    <thead>
                      <tr>
                        <th class="col-md-4">Filename</th>
                        <th class="col-md-2">Size</th>
                        <th class="col-md-6">Detail</th>
                      </tr>
                    </thead>
                    <tbody id="fsUploadProgress">
                    </tbody>
                </table>
            </div>
        </div>

        <table class="table" >
            <tr>
                <td>序号</td>
                <td>文件名</td>
                <td>大小</td>
                <td>类型</td>
                <td>上传时间</td>
                <td>操作</td>
            </tr>
        <?php foreach ($items as $key=>$item):?>
            <tr>
                <td><?=$key+1?></td>
                <td title="<?=$item['key']?>"><?=strlen($item['key'])>30?substr($item['key'], 0,30)."...":$item['key']?></td>
                <td><?=RealSize($item['fsize'])?></td>
                <td><?=$item['mimeType']?></td>
                <td><?=date("Y-m-d H:i:s",$item['putTime']/10000000)?></td>
                <td>
                    <a href="index.php?file=<?=$item['key']?>" target="_blank" class="btn btn-default btn-sm">查看</a>
                    <a href="qrcode.php?file=<?=$item['key']?>" target="_blank" class="btn btn-default btn-sm">二维码</a>
                    <a href="admin.php?delete_file=<?=$item['key']?>"  class="btn btn-default btn-sm btn-delete">删除</a>
                </td>
            </tr>
        <?php endforeach?>
        </table>
    </div>
</div>

<?php endif?>  

<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/plupload/plupload.full.min.js"></script>
<script type="text/javascript" src="js/plupload/i18n/zh_CN.js"></script>
<script type="text/javascript" src="js/ui.js"></script>
<script type="text/javascript" src="js/qiniu.js"></script>
<script type="text/javascript" src="js/highlight/highlight.js"></script>
<script type="text/javascript" src="js/main.js"></script>




</body>
</html>


<?php
function RealSize($size)
{
    if ($size < 1024)
    {
        return $size.' Byte';
    }
    if ($size < 1048576)
    {
        return round($size / 1024, 2).' KB';
    }
    if ($size < 1073741824)
    {
        return round($size / 1048576, 2).' MB';
    }
    if ($size < 1099511627776)
    {
        return round($size / 1073741824, 2).' GB';
    }
}
?>